import Vue from 'vue'
import 'babel-polyfill'
import 'normalize.css/normalize.css'// A modern alternative to CSS resets

import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import '@/styles/index.scss' // global css

import App from './App'
import router from './router'
import store from './store'

import i18n from './lang' // Internationalization
import './icons' // icon
import './errorLog'// error log
// import './permission' // permission control
// import './mock' // simulation data
import * as filters from './filters' // global filters

Vue.use(Element, {
  size: 'medium', // set element-ui default size
  i18n: (key, value) => i18n.t(key, value)
})

// register global utility filters.
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false

// router.beforeEach((to, from, next) => {
//   if (store.getters.token !== '') { // 判断是否有token
//   	console.log(store.getters.token)
//     if (to.path === '/login') {
//     	console.log(456)
//       next({ path: '/' });
//     } else {
//     	console.log(123)
//       store.dispatch('GetMenu').then(() => { // 生成可访问的路由表
//       	next()
//         // router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
//         // next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
//       })
//     }
//   } else {
//   	console.log(888)
//   	console.log(store.getters.token)
//   	next({ path: '/login' })
//   }
// });

router.beforeEach((to, from, next) => {
  if(to.matched.some(route => route.meta.requireAuth)) {
    console.log('path', to.path)
    if (store.getters.token) {
			if (to.path === '/') {
			 next({ path: '/index' })
      } else {
			 next()
			}
		} else {
			next({ path: '/login'})
		}
	} else {
		next()
	}
});

new Vue({
  el: '#app',
  router,
  store,
  i18n,
  template: '<App/>',
  components: { App }
})
